package org.x.casemodule.commom;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class DocumentExporter {
    public String export(String htmlContent, String format, String watermark) {
        // 实际应使用如Flying Saucer等库实现HTML转PDF
        // 这里简化为返回OSS路径
        return "https://oss.example.com/docs/" +
                System.currentTimeMillis() + "." + format.toLowerCase();
    }

    public String addSignatureToDocument(String content, String signImage, String format) {
        // 实际实现签名添加到文档的逻辑
        return "https://oss.example.com/signed/" +
                System.currentTimeMillis() + ".pdf";
    }

    public void exportToStream(String htmlContent, String format, String watermark, OutputStream outputStream) throws IOException {
        // 实际应使用如Flying Saucer等库实现HTML转PDF并写入输出流
        // 这里简化为写入模拟数据
        String content = "Document Content: " + htmlContent + "\nWatermark: " + watermark + "\nFormat: " + format;
        outputStream.write(content.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 将文档内容导出为字节数组
     * @param htmlContent HTML内容
     * @param format 文档格式 (pdf, word)
     * @param watermark 水印文本
     * @return 文档字节数组
     * @throws IOException IO异常
     */
    public byte[] exportToByteArray(String htmlContent, String format, String watermark) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 如果是PDF格式，使用iText等库生成真实的PDF文档
            if ("pdf".equalsIgnoreCase(format)) {
                // 示例：生成简单的PDF内容（实际应使用专业库）
                String pdfContent = "%PDF-1.4\n" +
                        "1 0 obj\n" +
                        "<< /Type /Catalog /Pages 2 0 R >>\n" +
                        "endobj\n" +
                        "2 0 obj\n" +
                        "<< /Type /Pages /Kids [3 0 R] /Count 1 >>\n" +
                        "endobj\n" +
                        "3 0 obj\n" +
                        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >>\n" +
                        "endobj\n" +
                        "4 0 obj\n" +
                        "<< /Length 44 >>\n" +
                        "stream\n" +
                        "BT\n" +
                        "/F1 12 Tf\n" +
                        "72 720 Td\n" +
                        "(Document Content: " + htmlContent + ") Tj\n" +
                        "0 -24 Td\n" +
                        "(Watermark: " + watermark + ") Tj\n" +
                        "ET\n" +
                        "endstream\n" +
                        "endobj\n" +
                        "xref\n" +
                        "0 5\n" +
                        "0000000000 65535 f \n" +
                        "0000000010 00000 n \n" +
                        "0000000053 00000 n \n" +
                        "0000000115 00000 n \n" +
                        "0000000205 00000 n \n" +
                        "trailer\n" +
                        "<< /Size 5 /Root 1 0 R >>\n" +
                        "startxref\n" +
                        "350\n" +
                        "%%EOF";

                baos.write(pdfContent.getBytes(StandardCharsets.UTF_8));
            }
            // 如果是Word格式，生成简单的Word文档内容
            else if ("word".equalsIgnoreCase(format) || "docx".equalsIgnoreCase(format)) {
                // 示例：生成简单的Word文档内容
                String wordContent = "PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000" +
                        "Document Content: " + htmlContent + "\n" +
                        "Watermark: " + watermark + "\n" +
                        "Format: " + format + "\n" +
                        "PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000";
                baos.write(wordContent.getBytes(StandardCharsets.UTF_8));
            }
            // 默认生成文本内容
            else {
                String content = "Document Content: " + htmlContent + "\nWatermark: " + watermark + "\nFormat: " + format;
                baos.write(content.getBytes(StandardCharsets.UTF_8));
            }
        } finally {
            baos.close();
        }
        return baos.toByteArray();
    }
}
